<?php
require_once ("../src/db/connectdatabase.php");

function getDatas($start, $lenght) {
	$con = new ConnectDB();
	
	$sql = "Select idques, question, dateupdate, isuser FROM loan_question_cong_tac_vien ORDER BY dateupdate DESC LIMIT ".$start.",".$lenght;
	return $con -> getvalueString($sql);
}

function getRowsData() {
	$con = new ConnectDB();
	
	$sql = "Select COUNT(*) FROM loan_question_cong_tac_vien";
	return $con -> getvalueString($sql);
}

function int_to_date($int)
{
    $time  = date("d/m/Y", $int);
    return $time;
}

$page = 1;
if(isset($_GET['page'])) {
	$page = $_GET['page'];
}

$arrcountrows = getRowsData();
$sumrecord = $arrcountrows[0][0];
$numrecord = 50;
$pageNumber = intval(($sumrecord / $numrecord));
if(($sumrecord % $numrecord) != 0) {
	$pageNumber++;
}

if($pageNumber == 0) {
	$pageNumber = 1;	
}

if($page > $pageNumber) {
	$page = $pageNumber;
}

//load data
$arrdata = getDatas(($page-1)*$numrecord, $numrecord);
?>
<style>
.listviewcontent table {
	width: 900px;
	max-width: 900px;
}

.listviewcontent table td {
	margin-top: 2px;
	border: 1px solid;
	border-radius: 2px 2px 2px 2px;
}

.title {
	text-align: center;
	background-color: #0088cc;
	min-height: 40px;
	-moz-border-radius: 8px 8px 0px 0px;
	-webkit-border-radius: 8px 8px 0px 0px;
	border-radius: 8px 8px 0px 0px;
	color: #FFF;
}

.listviewcontent table tr:hover td {
	background: #FFC;
}

.class-pagination{
		float: right;
		clear: left;
		padding-top: 20px;
	}
	
	.class-pageLabel{
		font-size: 11px;
		font-weight: bold;
		text-transform: uppercase;
	}

	.class-pageNow{
		padding: 5px;
		padding-top: 4px;
		margin: 2px;
		font-size: 11px;
		font-weight: bold;
		border: 1px solid #C0C0C0;
		background-color: #C0C0C0;
		border-radius: 4px;
		box-shadow: 0 0 10px rgba(0,0,0,.1);
		cursor: default;
	}
	
	.class-pageElem{
		padding: 5px;
		padding-top: 4px;
		margin: 2px;
		font-size: 11px;
		font-weight: bold;
		border: 1px solid #C0C0C0;
		box-shadow: 0 0 10px rgba(0,0,0,.2);
		border-radius: 4px;
		background-color: white;
	}

	.class-pageElem:hover{
		padding: 5px;
		margin: 2px;
		font-size: 11px;
		font-weight: bold;
		background-color: #C0C0C0;
		cursor: pointer;
	}
	.submitform {
		background: #004A00;
		text-indent: 1px;
		width:80px;
		height:30px;
		cursor: pointer;
		font-weight: bold;
		font-size: 14px;
	}
</style>
<h3>Danh Sách Câu hỏi Chương trình Cộng tác viên</h3>
<div>
	<div><a href="admin.php?content=quanlykhac&as=add-ct-cong-tac-vien"><img src="../images/icon-add.jpg" title="Thêm" />Thêm Câu Hỏi</a> &nbsp;&nbsp;  &nbsp;&nbsp;<a href="admin.php?content=quanlykhac&as=ct-cong-tac-vien"><img src="../images/icon-detail.gif" title="Thông tin giới thiệu" />Thông tin giới thiệu</a></div>
    <div style="clear:both; height:15px;"></div>
	<div class="class-pagination">
        <span class="class-pageLabel">Trang</span>
        <?php if($page == 1) { ?>
        <span title="Trang 1" page="1" class="class-pageNow">1</span>
        <?php } else { ?>
        <span title="Trang 1" page="1" class="class-pageElem">1</span>
        <?php }?>
        <?php if ($pageNumber != 1) { for($i = 2; $i <= $pageNumber; $i++) { ?>
        <?php if($i == $page) { ?>
        <span title="Trang <?php echo($i) ?>" page="<?php echo($i) ?>" class="class-pageNow"><?php echo($i) ?></span>
        <?php } else { ?>
        <span title="Trang <?php echo($i) ?>" page="<?php echo($i) ?>" class="class-pageElem"><?php echo($i) ?></span>
        <?php }?>
        <?php }}?>
    </div>
    <div style="clear:both; height:10px;"></div>
    <div class="listviewcontent">
    	<table>
        	<tr>
            	<th class="title" width="50px">STT</th>
                <th class="title" width="550px">Câu Hỏi</th>
            	<th class="title" width="100px">Ngày C.Nhật</th>
                <th class="title">Chức Năng</th>
            </tr>
            <?php
			$i = ($page-1)*$numrecord + 1;
			foreach($arrdata as $entry) {
			?>
            <tr>
            	<td align="center"><?php echo($i) ?></td>
                <td align="left"><?php echo($entry[1]) ?></td>
                <td align="center"><?php echo(int_to_date($entry[2])) ?></td>
                <td align="center"><?php if($entry[3] == "1") { ?><img class="class-display-link" idpr="<?php echo($entry[0]) ?>" style="cursor:pointer;" src="../images/unclockicon.png" height="20" title="Click để không cho phép hiển thị" /><?php } else { ?><img class="class-hiden-link" idpr="<?php echo($entry[0]) ?>" style="cursor:pointer;" src="../images/lockicon.png" title="Click để hiển thị trên trang chủ" height="20" /><?php } ?>&nbsp; | &nbsp;<a href="admin.php?content=quanlykhac&as=edit-ct-cong-tac-vien&id=<?php echo($entry[0]) ?>"><img src="../images/icon_detail_cardiscount.png" height="20" title="Cập Nhật Thông Tin" /></a>&nbsp; | &nbsp;<img idpr="<?php echo($entry[0]) ?>" class="class-deletequestion-link" src="../images/icon-delete.gif" height="20" title="Xóa" /></td>
            </tr>
            <?php
				$i++;
			}
			?>
        </table>
    </div>
    <div style="clear:both; height:15px;"></div>
    <div class="class-pagination">
        <span class="class-pageLabel">Trang</span>
        <?php if($page == 1) { ?>
        <span title="Trang 1" page="1" class="class-pageNow">1</span>
        <?php } else { ?>
        <span title="Trang 1" page="1" class="class-pageElem">1</span>
        <?php }?>
        <?php if ($pageNumber != 1) { for($i = 2; $i <= $pageNumber; $i++) { ?>
        <?php if($i == $page) { ?>
        <span title="Trang <?php echo($i) ?>" page="<?php echo($i) ?>" class="class-pageNow"><?php echo($i) ?></span>
        <?php } else { ?>
        <span title="Trang <?php echo($i) ?>" page="<?php echo($i) ?>" class="class-pageElem"><?php echo($i) ?></span>
        <?php }?>
        <?php }}?>
    </div>
    <div style="clear:both; height:15px;"></div>
    <div><a href="admin.php?content=quanlykhac&as=add-ct-cong-tac-vien"><img src="../images/icon-add.jpg" title="Thêm Câu Hỏi Khi Làm Thẻ Tín Dụng" />Thêm Câu Hỏi</a></div>
</div>
<script>
$(document).on('click', '.class-pageElem', function() {
	page = $(this).attr('page');
	
	var bank = $('.bankloadselect').val();
	var md = $('#form_search_mucdich').val();
	
	openwindowns('admin.php?content=quanlykhac&as=question-ct-cong-tac-vien' + '&page=' + page);
});

function openwindowns(url) {
	window.location = url;
}


$(document).on('click', '.class-display-link', function() {
	elem = $(this);
	id = elem.attr('idpr');
	$.ajax({
		type: "POST",
		url: "../src/ajax/AjaxAdminCongTacVien.php",
		data:{act: 'edith',type: "0", idpe: id},
		dataType: "json",
		success: function(response){
			if(response == true) {
				alert("Cập nhật thành công");
				elem.parent().html('<img class="class-hiden-link" height="20" idpr="' + id + '" style="cursor:pointer;" src="../images/lockicon.png" title="Click để hiển thị trên trang chủ" />&nbsp; | &nbsp;<a href="admin.php?content=quanlykhac&as=edit-ct-cong-tac-vien&id=' + id + '"><img src="../images/icon_detail_cardiscount.png" height="20" title="Cập Nhật Thông Tin" /></a>&nbsp; | &nbsp;<img idpr="' + id + '" class="class-deletequestion-link" src="../images/icon-delete.gif" height="20" title="Xóa" />');
			} else {
				alert("Cập nhật thất bại");
			}
		}
	});
});

$(document).on('click', '.class-hiden-link', function() {
	elem = $(this);
	id = elem.attr('idpr');
	$.ajax({
		type: "POST",
		url: "../src/ajax/AjaxAdminCongTacVien.php",
		data:{act: 'edith',type: "1", idpe: id},
		dataType: "json",
		success: function(response){
			if(response == true) {
				alert("Cập nhật thành công");
				elem.parent().html('<img class="class-display-link" height="20" idpr="' + id + '" style="cursor:pointer;" src="../images/unclockicon.png" title="Click để không cho phép hiển thị" />&nbsp; | &nbsp;<a href="admin.php?content=quanlykhac&as=edit-ct-cong-tac-vien&id=' + id + '"><img src="../images/icon_detail_cardiscount.png" height="20" title="Cập Nhật Thông Tin" /></a>&nbsp; | &nbsp;<img idpr="' + id + '" class="class-deletequestion-link" src="../images/icon-delete.gif" height="20" title="Xóa" />');
			} else {
				alert("Cập nhật thất bại");
			}
		}
	});
});

$(document).on('click', '.class-deletequestion-link', function() {
	elem = $(this);
	id = elem.attr('idpr');
	if(confirm('Bạn thực sự muốn xóa?')) {
		$.ajax({
			type: "POST",
			url: "../src/ajax/AjaxAdminCongTacVien.php",
			data:{act: 'del', idpe: id},
			dataType: "json",
			success: function(response){
				if(response == true) {
					alert("Cập nhật thành công");
					elem.parent().parent().remove();
				} else {
					alert("Cập nhật thất bại");
				}
			}
		});
	}
});
</script>